package com.genesis.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.Result;
import com.genesis.mall.common.SmsAdvertisePage;
import com.genesis.mall.entity.SmsHomeAdvertise;
import com.genesis.mall.service.ISmsHomeAdvertiseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页轮播广告表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/home/advertise")
@Slf4j
public class SmsHomeAdvertiseController {

    @Autowired
    private ISmsHomeAdvertiseService smsHomeAdvertiseService;


    /**
     * 首页广告展示
     * @param pageNum
     * @param pageSize
     * @param
     * @return
     */
    @GetMapping("/list")
    public Result<SmsAdvertisePage<SmsHomeAdvertise>> list(Long pageNum, Long pageSize, String name, String endTime,Integer type){
        log.info("end",endTime);

        Page<SmsHomeAdvertise> pageInfo= new Page(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeAdvertise> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),SmsHomeAdvertise::getName,name);
        queryWrapper.eq(type!=null,SmsHomeAdvertise::getType,type);
        queryWrapper.lt(endTime!=null,SmsHomeAdvertise::getEndTime,endTime);
        queryWrapper.orderByDesc(SmsHomeAdvertise::getStartTime);
       smsHomeAdvertiseService.page(pageInfo,queryWrapper);

       SmsAdvertisePage<SmsHomeAdvertise> smsAdvertisePage= new SmsAdvertisePage<>();
        BeanUtils.copyProperties(pageInfo,smsAdvertisePage);

        return Result.success(smsAdvertisePage);

    }

    @PostMapping("/create")
    public Result<String>create(@RequestBody SmsHomeAdvertise smsHomeAdvertise){
        smsHomeAdvertiseService.save(smsHomeAdvertise);
        return Result.success("新增成功");
    }

    @GetMapping("{id}")
    public Result<SmsHomeAdvertise>getById(@PathVariable Long id){
        SmsHomeAdvertise advertise = smsHomeAdvertiseService.getById(id);
        return Result.success(advertise);
    }


    @PostMapping("/update/{id}")
    public Result<String>update(@PathVariable Long id,@RequestBody SmsHomeAdvertise smsHomeAdvertise){
        smsHomeAdvertiseService.updateById(smsHomeAdvertise);
        return Result.success("修改成功");
    }

    @PostMapping("/delete")
    public Result<String>delete(@RequestParam List<Integer> ids){
        smsHomeAdvertiseService.removeByIds(ids);
        return Result.success("删除成功");
    }







}
